package com.yunxi.controller.sat;

import com.github.pagehelper.util.StringUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sat.pojo.SatCustomerFollowVO;
import com.yunxi.sat.service.SatCustomerFollowDubboService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 客户跟进记录表 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-07-11
 */
@RestController
@RequestMapping("/admin/satCustomerFollow")
@Api(tags = "sat:customerFollow", description = "客户跟进记录表接口")
public class SatCustomerFollowController {
    private static final Logger logger = LoggerFactory.getLogger(SatCustomerFollowController.class);

    @Autowired
    SatCustomerFollowDubboService satCustomerFollowDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult page(SatCustomerFollowVO satCustomerFollowVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerFollowDubboService.findPageByVO(satCustomerFollowVO, pageNum, pageSize,loginInfo));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    @ApiImplicitParam(paramType="query", name = "followId", value = "主键id", required = true, dataType = "String")
    public ApiResult load(String followId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerFollowDubboService.getById(followId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody SatCustomerFollowVO satCustomerFollowVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerFollowDubboService.saveOrUpdate(satCustomerFollowVO,loginInfo);
        return ResponseUtil.ok("success");
    }
    @PostMapping("/addFollow")
    @ApiOperation(value = "新增跟进")
    public ApiResult addFollow(@RequestBody SatCustomerFollowVO satCustomerFollowVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerFollowDubboService.addFollow(satCustomerFollowVO,loginInfo);
        return ResponseUtil.ok("success");
    }
    @PostMapping("/complete")
    @ApiOperation(value = "跟进完成")
    public ApiResult complete(@RequestBody SatCustomerFollowVO satCustomerFollowVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerFollowDubboService.complete(satCustomerFollowVO,loginInfo);
        return ResponseUtil.ok("success");
    }


    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody @ApiParam(value = "主键id集合") List<String> list,@CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerFollowDubboService.del(list,loginInfo.getUserCode());
        return ResponseUtil.ok("success");
    }

    @GetMapping("/getFollowsByNo")
    @ApiOperation(value = "根据客户编号查询历史跟进")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = false, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = false, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "isPage", value = "是否分页(1是)", required = false, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "customerNo", value = "客户编号", required = true, dataType = "String")
    })
    public ApiResult getFollowsByNo(String customerNo, @CurrentUser LoginInfo loginInfo,Integer pageNum,Integer pageSize,Integer isPage) throws Exception {
        if(isPage != null && isPage.equals(1)){
            return ResponseUtil.ok(satCustomerFollowDubboService.getFollowsByNoForPage(customerNo,loginInfo,pageNum,pageSize));
        }
        return ResponseUtil.ok(satCustomerFollowDubboService.getFollowsByNo(customerNo,loginInfo));
    }

    @GetMapping("/waitFollow")
    @ApiOperation(value = "待跟进客户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "planFollowTimeStr", value = "计划跟进时间", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "isPage", value = "是否分页(1是)", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = false, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = false, dataType = "int")
    })
    public ApiResult waitFollow(SatCustomerFollowVO satCustomerFollowVO,String isPage,Integer pageNum,Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        //传计划跟进时间则按天查询,不传则查询所有
        if(StringUtil.isEmpty(isPage)){
            return ResponseUtil.ok(satCustomerFollowDubboService.waitFollow(satCustomerFollowVO,loginInfo));
        }
        else if(isPage.equals("1")){
            return ResponseUtil.ok(satCustomerFollowDubboService.waitFollowForPage(satCustomerFollowVO,loginInfo,pageNum,pageSize));
        }
        return ResponseUtil.ok("success");
    }

    @GetMapping("/lateFollow")
    @ApiOperation(value = "逾期未跟进客户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "isPage", value = "是否分页(1是)", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = false, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = false, dataType = "int")
    })
    public ApiResult lateFollow(SatCustomerFollowVO satCustomerFollowVO,@CurrentUser LoginInfo loginInfo,String isPage,Integer pageNum,Integer pageSize) throws Exception {
        if(StringUtil.isEmpty(isPage)){
            return ResponseUtil.ok(satCustomerFollowDubboService.lateFollow(satCustomerFollowVO,loginInfo));
        }
        else if(isPage.equals("1")){
            return ResponseUtil.ok(satCustomerFollowDubboService.lateFollowForPage(satCustomerFollowVO,loginInfo,pageNum,pageSize));
        }
        return ResponseUtil.ok("success");
    }

    @GetMapping("/goodCustomerFollow")
    @ApiOperation(value = "老客户跟进")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "isPage", value = "是否分页(1是)", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = false, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = false, dataType = "int")
    })
    public ApiResult goodCustomerFollow(SatCustomerFollowVO satCustomerFollowVO,@CurrentUser LoginInfo loginInfo,String isPage,Integer pageNum,Integer pageSize) throws Exception {
        if(StringUtil.isEmpty(isPage)){
            return ResponseUtil.ok(satCustomerFollowDubboService.goodCustomerFollow(satCustomerFollowVO,loginInfo));
        }
        else if(isPage.equals("1")){
            return ResponseUtil.ok(satCustomerFollowDubboService.goodCustomerFollowForPage(satCustomerFollowVO,loginInfo,pageNum,pageSize));
        }
        return ResponseUtil.ok("success");
    }

    @GetMapping("/followRemind")
    @ApiOperation(value = "今日跟进提醒报表")
    public ApiResult followRemind(@CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerFollowDubboService.followRemind(loginInfo));
    }

    @GetMapping("/getWaitFollowsByNo")
    @ApiOperation(value = "根据客户编号查询待跟进")
    @ApiImplicitParam(paramType="query", name = "customerNo", value = "客户编号", required = true, dataType = "String")
    public ApiResult getWaitFollowsByNo(String customerNo, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerFollowDubboService.getWaitFollowsByNo(customerNo,loginInfo));
    }

    @GetMapping("/getWaitFollowCount")
    @ApiOperation(value = "查询今日及后5天待跟进数量")
    public ApiResult getWaitFollowCount(@CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerFollowDubboService.getWaitFollowCount(loginInfo));
    }

}
